package com.example.petstore_2.controller;

import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.Item;
import com.example.petstore_2.domain.Product;
import com.example.petstore_2.service.ItemService;
import com.example.petstore_2.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 获取某个商品
     * @param itemId
     * @return
     */
    @GetMapping("/items/{itemId}")
    public CommonResponse getItemById(@PathVariable("itemId") String itemId){
        return itemService.getItemByItemid(itemId);
    }

    /**
     * 获取items根据productid
     * @param productId
     * @return
     */
    @GetMapping("/items/product/{productId}")
    public CommonResponse getItemByProduct(@PathVariable("productId") String productId){
        List<Item> itemList = itemService.getItemListByProductId(productId);
        return CommonResponse.creatForSuccess(itemList);
    }
}